package com.work.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.work.work.entity.Role;
import com.work.work.mapper.RoleMapper;
import com.work.work.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Override
    @Transactional
    public boolean createRole(Role role) {
        if (role.getRolename() == null || role.getRolename().trim().isEmpty()) {
            return false;
        }
        return save(role);
    }

    @Override
    @Transactional
    public boolean updateRole(Role role) {
        if (role.getId() == null || role.getRolename() == null || role.getRolename().trim().isEmpty()) {
            return false;
        }
        return updateById(role);
    }

    @Override
    public Page<Role> getRoleList(Integer current, Integer size) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Role::getId);
        return page(new Page<>(current, size), queryWrapper);
    }

    @Override
    public List<Role> getAllRoles() {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Role::getId);
        return list(queryWrapper);
    }

    @Override
    public List<Role> searchRoles(String keyword) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Role::getRolename, keyword)
                .or()
                .like(Role::getDescription, keyword);
        return list(queryWrapper);
    }
}